package com.ebc.eservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ebc.eservice.mapper.CompanyArticleMapper;
import com.ebc.eservice.mapper.CompanyArticleTypeMapper;
import com.ebc.eservice.pojo.EbcCompanyArticle;
import com.ebc.eservice.pojo.EbcCompanyArticleType;
import com.ebc.eservice.pojo.EbcUser;
import com.ebc.eservice.pojo.SingleIntroduction;
import com.ebc.eservice.pojo.pic.UploadFile;
import com.ebc.eservice.service.EbcArticleService;
import com.ebc.eservice.util.JsonResult;
import com.ebc.eservice.vo.admin.CompanyAccountRespVo;
import com.ebc.eservice.vo.article.AddArticleTypeReqVo;
import com.ebc.eservice.vo.article.ArticleReqVo;
import com.ebc.eservice.vo.article.ArticleRespVo;
import com.ebc.eservice.vo.article.ArticleTypeRespVo;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author: Joey
 * @Description:
 * @date:2022/9/25 16:52
 */
@Service
public class ArticleImp implements EbcArticleService {

    @Autowired
    private CompanyArticleTypeMapper companyArticleTypeMapper;
    @Autowired
    private CompanyArticleTypeMapper TypeArticleMapper;
    @Autowired
    private CompanyArticleMapper companyArticleMapper;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addNewArticle(ArticleReqVo articleReqVo) {

        EbcCompanyArticle article = new EbcCompanyArticle();
        //todo 文章的公司id
        article.setCompanyId(articleReqVo.getCompanyId())
                .setTitle(articleReqVo.getTitle())
                .setContent(articleReqVo.getContent())
                .setOpen(1)
                .setTypeId(articleReqVo.getTypeId())
                .setCreateId(articleReqVo.getCreateId())
                .setRealViewNum(0)
                .setViewNum(articleReqVo.getViewNum());
        companyArticleMapper.insert(article);

        //查询当前增加的文章
        EbcCompanyArticle articleOne=companyArticleMapper.selectById(article.getId());
        return JsonResult.success(articleOne.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addArticlePic(Integer articleId, MultipartFile articleFile) throws IOException {
        System.out.println("文章"+articleId+",添加照片...");
        JsonResult result=null;
        Query query = new Query(Criteria.where("articleId").is(articleId));
        UploadFile file = mongoTemplate.findOne(query,UploadFile.class);
        if(file!=null){ //更新
            file.setContent(new Binary(articleFile.getBytes()));
            file.setName(articleFile.getOriginalFilename());
            file.setUpdateTime(new Date());
            file.setArticleId(articleId);
            file.setContentType(articleFile.getContentType());
            file.setSize(articleFile.getSize());
            mongoTemplate.save(file);
            result = JsonResult.success("文章id-> "+articleId+",照片更新");
        }else{  //新增
            UploadFile uploadFile = new UploadFile();
            uploadFile.setContent(new Binary(articleFile.getBytes()));
            uploadFile.setName(articleFile.getOriginalFilename());
            uploadFile.setCreatedTime(new Date());
            uploadFile.setArticleId(articleId);
            uploadFile.setContentType(articleFile.getContentType());
            uploadFile.setSize(articleFile.getSize());
            UploadFile save = mongoTemplate.save(uploadFile);
            result = JsonResult.success("文章id-> "+articleId+",照片新增");
        }
        return result;
    }

    @Override
    public JsonResult getArticleList(int current, int size,int companyId) {
        //后台-
//        EbcCompanyArticle article = new EbcCompanyArticle();
//        Page<EbcCompanyArticle> page = new Page<>(current, size);
//        QueryWrapper<EbcCompanyArticle> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("company_id",companyId);
//        companyArticleMapper.selectPage(page,queryWrapper);
//        List<EbcCompanyArticle> articles = page.getRecords();

        Page<ArticleRespVo> page = new Page<>(current,size,false);
        IPage<ArticleRespVo> list = companyArticleMapper.getArticleList(page,companyId);
        List<ArticleRespVo> records = list.getRecords();
        return JsonResult.success(records);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult updateArticle(ArticleRespVo articleRespVo) throws IOException {
        JsonResult result=null;
        EbcCompanyArticle article = companyArticleMapper.selectById(articleRespVo.getId());
        article.setTitle(articleRespVo.getTitle())
                .setContent(articleRespVo.getContent())
                .setTypeId(articleRespVo.getTypeId())
                .setViewNum(articleRespVo.getViewNum());
        companyArticleMapper.updateById(article);
        if(articleRespVo.getPicFile()!=null){
            Query query = new Query(Criteria.where("articleId").is(articleRespVo.getId()));
            UploadFile file = mongoTemplate.findOne(query,UploadFile.class);
            if(file!=null){ //更新
                MultipartFile articleFile =  articleRespVo.getPicFile();
                file.setContent(new Binary(articleFile.getBytes()));
                file.setName(articleFile.getOriginalFilename());
                file.setUpdateTime(new Date());
                file.setArticleId(articleRespVo.getId());
                file.setContentType(articleFile.getContentType());
                file.setSize(articleFile.getSize());
                mongoTemplate.save(file);
                result = JsonResult.success("文章更新成功,id-> "+articleRespVo.getId()+",照片更新");
            }else{
                UploadFile uploadFile = new UploadFile();
                uploadFile.setContent(new Binary(articleRespVo.getPicFile().getBytes()));
                uploadFile.setName(articleRespVo.getPicFile().getOriginalFilename());
                uploadFile.setCreatedTime(new Date());
                uploadFile.setArticleId(articleRespVo.getId());//introId
                uploadFile.setContentType(articleRespVo.getPicFile().getContentType());
                uploadFile.setSize(articleRespVo.getPicFile().getSize());
                mongoTemplate.save(uploadFile);
                result = JsonResult.success("文章-> "+articleRespVo.getId()+",照片新增");
            }
        }



        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult deleteArticle(Integer aid) {
        int i = companyArticleMapper.deleteById(aid);
        if(i>0){
            return JsonResult.success("删除成功");
        }else{
            return JsonResult.fail("删除失败,参数异常");
        }
    }

    @Override
    public JsonResult getArticleType() {
        List<ArticleTypeRespVo> respVos = companyArticleTypeMapper.ArticleTypeList();
        return JsonResult.success(respVos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addArticleType(AddArticleTypeReqVo articleTypeReqVo) {
        EbcCompanyArticleType type = new EbcCompanyArticleType();
        type.setType(articleTypeReqVo.getTypeName()).setCreateId(articleTypeReqVo.getCreateId());
        companyArticleTypeMapper.insert(type);
        return JsonResult.success("添加成功");
    }

    @Override
    public JsonResult editArticleType(AddArticleTypeReqVo articleTypeReqVo) {
        EbcCompanyArticleType type = companyArticleTypeMapper.selectById(articleTypeReqVo.getTypeId());
        type.setType(articleTypeReqVo.getTypeName());
        companyArticleTypeMapper.updateById(type);
        return JsonResult.success("修改成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult deleteArticleType(AddArticleTypeReqVo Type) {
        //id  根据id查 文章 如果有 不删除
        QueryWrapper<EbcCompanyArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("type_id",Type.getTypeId());
        List<EbcCompanyArticle> companyArticles = companyArticleMapper.selectList(wrapper);
        if(companyArticles.size()>0){
            return JsonResult.fail("请先删除文章再删除类型。");
        }else{
            companyArticleTypeMapper.deleteById(Type.getTypeId());
            return JsonResult.success("删除成功: id="+Type.getTypeId());
        }
    }
}
